package cn.jianml.captcha.controller;

import cn.jianml.captcha.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wujian
 * @time 2020/1/7
 */
@RestController
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        captchaService.create(request, response);
    }

    @PostMapping("/login")
    public String login(String username, String password, String code, String key){
        // 省略了用户账号信息的验证逻辑...
        try {
            captchaService.check(key, code);
            return "验证成功";
        }catch (Exception e){
            // 可以用全局异常处理来优化此处代码
            return e.getMessage();
        }
    }

}
